﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Interop;

namespace WebAnalyser.UI
{
    public class TaskbarManager
    {
        private TaskbarManager()
        {

        }

        private static readonly Object SyncLock = new Object();

        private static volatile TaskbarManager _instance;
        public static TaskbarManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (SyncLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new TaskbarManager();
                        }
                    }
                }
                return _instance;
            }
        }

        private ITaskbarList3 _taskbarList;
        internal ITaskbarList3 TaskbarList
        {
            get
            {
                if (_taskbarList == null)
                {
                    lock (SyncLock)
                    {
                        if (_taskbarList == null)
                        {
                            _taskbarList = (ITaskbarList3)new CTaskbarList();
                            _taskbarList.HrInit();
                        }
                    }
                }
                return _taskbarList;
            }
        }

        private IntPtr _ownerHandle;
        internal IntPtr OwnerHandle
        {
            get
            {
                if (_ownerHandle == IntPtr.Zero)
                {
                    Process currentProcess = Process.GetCurrentProcess();

                    if (currentProcess != null && currentProcess.MainWindowHandle != IntPtr.Zero)
                    {
                        _ownerHandle = currentProcess.MainWindowHandle;
                    }
                    else
                    {
                        throw new InvalidOperationException("A valid active Window is needed to update the Taskbar");
                    }
                }
                return _ownerHandle;
            }
        }

        public void SetProgressState(TaskbarProgressBarState state)
        {
            TaskbarList.SetProgressState(OwnerHandle, (TBPFLAG)state);
        }

        public void SetProgressState(TaskbarProgressBarState state, Window window)
        {
            TaskbarList.SetProgressState((new WindowInteropHelper(window)).Handle, (TBPFLAG)state);
        }

        public void SetProgressValue(int currentValue, int maximumValue)
        {
            TaskbarList.SetProgressValue(OwnerHandle, Convert.ToUInt32(currentValue), Convert.ToUInt32(maximumValue));
        }

        public void SetProgressValue(int currentValue, int maximumValue, Window window)
        {
            TaskbarList.SetProgressValue((new WindowInteropHelper(window)).Handle, Convert.ToUInt32(currentValue), Convert.ToUInt32(maximumValue));
        }
    }
}
